#ifndef _MIX_THREAD_SPINLOCK_H_
#define _MIX_THREAD_SPINLOCK_H_

#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else
#include <pthread.h>
#endif

namespace mix
{

class SpinLock
{
public:
    SpinLock();
    ~SpinLock();

    void lock();
    void unlock();

    bool tryLock();

    class Guard
    {
        SpinLock& _locker;
    public:
        Guard( SpinLock& locker) : _locker(locker) {
            _locker.lock();
        }

        ~Guard() {
            _locker.unlock();
        }
    };

private:
    SpinLock( const SpinLock&);
    SpinLock& operator = ( const SpinLock&);

#if defined(_WIN32) || defined(_WIN64)
    long _spin;
#else
    pthread_spinlock_t _spin;
#endif
};

}   // namespace mix

#endif  // _MIX_THREAD_SPINLOCK_H
